package com.kgc.skconsumer.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesCollectionRefund;
import com.kgc.collection.pojo.WriteOff;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import com.kgc.skconsumer.feign.WriteOffFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/WriteOffController")
public class WriteOffController {

    private Logger logger = LoggerFactory.getLogger(WriteOffController.class);

    @Autowired
    private WriteOffFeign writeOffFeign;

    @Autowired
    private CwWorkFlowFeign cwWorkFlowFeign;


    @RequestMapping("/selectWriteOffType")
    public ResponseMsg selectWriteOffType(){
        logger.info("WriteOffController selectWriteOffType is start");
        ResponseMsg responseMsg = writeOffFeign.selectWriteOffType();
        logger.debug("WriteOffController selectWriteOffType is end return {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectWriteOffByPage")
    public  ResponseMsg selectWriteOffByPage(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController selectWriteOffByPage is start params is {}",map.toString());
        ResponseMsg responseMsg = writeOffFeign.selectWriteOffByPage(map);
        logger.debug("WriteOffController selectWriteOffByPage is end return {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertWriteOff")
    public ResponseMsg insertWriteOff(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController insertWriteOff is start params is {}",map.toString());
        ResponseMsg responseMsg = writeOffFeign.insertWriteOff(map);
        ObjectMapper mapper = new ObjectMapper();
        logger.debug("WriteOffController insertWriteOff is end return {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateWriteOff")
    public  ResponseMsg updateWriteOff(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController updateWriteOff is start params is {}",map.toString());
        ResponseMsg responseMsg = writeOffFeign.updateWriteOff(map);
        logger.debug("WriteOffController updateWriteOff is end return {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/deleteWriteOff")
    public  ResponseMsg deleteWriteOff(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController deleteWriteOff is start params is {}",map.toString());
        ResponseMsg responseMsg = writeOffFeign.deleteWriteOff(map);
        logger.debug("WriteOffController deleteWriteOff is end return {}", responseMsg);
        return responseMsg;
    }


    @RequestMapping("/checkWriteOff")
    public  ResponseMsg checkWriteOff(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController checkWriteOff is start params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("WriteOffController checkWriteOff is end return {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectWriteOffByCode")
    public  ResponseMsg selectWriteOffByCode(@RequestBody Map<String,Object> map){
        logger.info("WriteOffController selectWriteOffByCode is start params is {}",map.toString());
        ResponseMsg responseMsg=writeOffFeign.selectWriteOffByCode(map);
        logger.debug("WriteOffController selectWriteOffByCode is end return {}", responseMsg);
        return responseMsg;
    }

}
